package com.kedou.sdk.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.text.SpannableString;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.kedou.sdk.ui.activity.UserLoginActivity;
import com.kedou.sdk.ui.assist.LoginTypeSwitchAdapter;
import com.kedou.sdk.ui.assist.LoginTypeSwitchWatcher;
import com.kedou.sdk.ui.assist.OnFinishActivityListener;
import com.kedou.sdk.ui.view.ExViewPager;
import com.kedou.sdk.utils.ResUtils;

/**
 * Created by Administrator on 2018/3/7.
 */

public class LoginDialogFragment extends BaseDialogFragment implements View.OnClickListener, ViewPager.OnPageChangeListener, LoginTypeSwitchWatcher.OnLoginTypeSwitchChangeListener {

    private static final String INIT_LOGIN_TYPE = "init_login_type";

    private ImageButton mCloseDialogBtn;
    private TextView registBtn;
    private ExViewPager mLoginTypeViewPager;

    private LoginTypeSwitchWatcher mLoginTypeSwitchWatcher;

    public static final LoginDialogFragment newInstance(int loginType) {
        Log.e("loginType","newInstance."+loginType);
        LoginDialogFragment fragment = new LoginDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(INIT_LOGIN_TYPE, loginType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View contentView = doInflateContentView(inflater, container, getLayoutIdentifier("wan_activity_login"));

//        TextView masterTextView = contentView.findViewById(getViewIdentifier("activity_login_title_text"));
        TextView slaveTextView = contentView.findViewById(getViewIdentifier("activity_login_switch_text"));
        SpannableString content = new SpannableString(ResUtils.getString(getContext(), "hnssn_login_phone_login"));

        mLoginTypeSwitchWatcher = new LoginTypeSwitchWatcher(slaveTextView);
        mLoginTypeSwitchWatcher.setOnLoginTypeSwitchChangeListener(this);

        registBtn = contentView.findViewById(getViewIdentifier("activity_login_goregist"));
        registBtn.setOnClickListener(this);
        mCloseDialogBtn = contentView.findViewById(getViewIdentifier("activity_login_finish_btn"));
        mLoginTypeViewPager = contentView.findViewById(getViewIdentifier("activity_login_type_viewpager"));
        mLoginTypeViewPager.setGestureSlideEnable(true);

        mLoginTypeViewPager.addOnPageChangeListener(this);

        //特殊处理，只开启手机登录开始 mOneKeyLoginBtn.setVisibility(View.GONE);
//        mLoginTypeViewPager.setAdapter(new LoginTypeSwitchOnlyPhoneAdapter(getChildFragmentManager()));
//        masterTextView.setText(ResUtils.getString(getContext(), "hnssn_login_switch_cellphone"));
//        contentView.findViewById(getViewIdentifier("activity_login_switch_icon")).setVisibility(View.GONE);
//        slaveTextView.setVisibility(View.GONE);
//        mOneKeyLoginBtn.setVisibility(WanSDK.getInstance().isShowQuickRegister() ? View.GONE : View.GONE);

//        if(XmlUtils.isPutInChannel()&&XmlUtils.isPutInChannel()&&"3.1.12".equals(XmlUtils.getSdkVersionName())){
//        }else {
//            mOneKeyLoginBtn.setVisibility(WanSDK.getInstance().isShowQuickRegister() ? View.VISIBLE : View.GONE);
//        }



            mLoginTypeViewPager.setAdapter(new LoginTypeSwitchAdapter(getChildFragmentManager()));

        int loginType = getArguments().getInt(INIT_LOGIN_TYPE);
        mLoginTypeSwitchWatcher.setCurrentLoginType(loginType);
        Log.e("loginType","fragment."+loginType);
        mLoginTypeViewPager.setCurrentItem(getNotNullArguments().getInt(INIT_LOGIN_TYPE, LoginTypeSwitchWatcher.SWITCH_MODE_WHICH_ACCOUNT));

        mCloseDialogBtn.setOnClickListener(this);



        return contentView;
    }

    private OnUserActionListener getOnUserActionListener() {
        if (!isNullContext()) {
            Activity activity = getActivity();
            if (activity instanceof OnUserActionListener) {
                return (OnUserActionListener) activity;
            }
        }
        return null;
    }
    private void nextToRegisterActivity() {
        LoginDialogFragment.OnUserActionListener listener = getOnUserActionListener();
        if (listener != null) {
            listener.doNextToQuickRegister();
        }
    }
    @Override
    public void onClick(View view) {
        if (view == mCloseDialogBtn) {
            finishActivity();
        }else if (view == registBtn){
            nextToRegisterActivity();
        }
//        else if (view == mOneKeyLoginBtn) {
//            if(XmlUtils.isNeedPrivate()){
//                boolean isChecked ;
//                Fragment fragment = ((FragmentPagerAdapter)mLoginTypeViewPager.getAdapter()).getItem(mLoginTypeViewPager.getCurrentItem());
//                if(fragment instanceof PhoneLoginFragment){
//                    isChecked = ((PhoneLoginFragment) fragment).isChecked();
//                }else {
//                    isChecked = ((AccountLoginFragment) fragment).isChecked();
//                }
//                if(!isChecked){
//                    Toast.makeText(getActivity(),"请先阅读并同意协议",Toast.LENGTH_SHORT).show();
//                }else {
//                    mOneKeyLoginBtn.setEnabled(false);
//                    mOneKeyLoginBtn.setClickable(false);
//                    nextToQuickRegisterActivity();
//                }
//
//            }else {
//                mOneKeyLoginBtn.setEnabled(false);
//                mOneKeyLoginBtn.setClickable(false);
//                nextToQuickRegisterActivity();
//            }
//
//        }
    }

    private void nextToQuickRegisterActivity() {
        OnUserActionListener listener = getOnUserActionListener();
        if (listener != null) {
            listener.doNextToOneKeyLogin();
        }
    }

    private void finishActivity() {
        OnUserActionListener listener = getOnUserActionListener();
        if (listener != null) {
            listener.doFinishActivity(this);
        }
    }

    @Override
    public void OnLoginTypeSwitchChanged(View view, int which) {
        mLoginTypeViewPager.setCurrentItem(which, true);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (mLoginTypeSwitchWatcher != null) {
            mLoginTypeSwitchWatcher.setCurrentLoginType(position);
        }

        if (!isNullContext()) {
            Activity activity = getActivity();
            if (activity instanceof UserLoginActivity) {
                ((UserLoginActivity) activity).setCurrentLoginType(position);
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public interface OnUserActionListener extends OnFinishActivityListener {

        void doSubmitLoginInfo(String username, String password);

        void doSubmitCellPhoneNumber(String cellphone, String verify);

        void doNextToOneKeyLogin();

        void doNextToQuickRegister();

        void doNextToChangePassword(String username);
        void doNextToLoginMore();
        void doNextToAppLogin();


    }

}
